package cn.yikaojiuguo.student.ui.my;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.support.simple.SimpleTextWatcher;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.utils.MD5Util;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.utils.Util;
import cn.yikaojiuguo.student.support.volley.Response;
import cn.yikaojiuguo.student.ui.base.CustomerActionBarActivity;

public class ChangePwdActivity extends CustomerActionBarActivity {

    @Bind(R.id.et_pwd1)
    EditText mEt1;
    @Bind(R.id.et_pwd2)
    EditText mEt2;
    @Bind(R.id.btn_commit)
    Button mBtn;
    private boolean mFlag1 = false;
    private boolean mFlag2 = false;

    public static Intent newIntent() {
        Intent intent = new Intent(YiKaoApplication.getInstance(),ChangePwdActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        ButterKnife.bind(this);
        initEvents();
    }

    private void initEvents() {
        setCommitButtonState(mFlag1 && mFlag2);
        mEt1.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() >= 6) {
                    mFlag1 = true;
                } else {
                    mFlag1 = false;
                }
                setCommitButtonState(mFlag1 && mFlag2);
            }
        });
        mEt2.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() >= 6) {
                    mFlag2 = true;
                } else {
                    mFlag2 = false;
                }
                setCommitButtonState(mFlag1 && mFlag2);
            }
        });
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(checkData()) {
                    String pwd = mEt1.getText().toString().trim();
                    String requestUrl = UrlConstants.MODIFY_PWD + "&mid=" + YiKaoApplication
                     .getUserId() + "&pwd=" + MD5Util.encode(pwd);
                    RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            if(response!=null && response.optBoolean("success")) {
                                ToastUtil.showToast(ChangePwdActivity.this,"密码修改成功");
                                finish();
                            }
                        }
                    }, null, mTag);
                }
            }
        });
        Util.openSoftInput(getWindow().getDecorView());
    }

    private boolean checkData() {
        String pwd1 = mEt1.getText().toString().trim();
        String pwd2 = mEt2.getText().toString().trim();
        if(TextUtils.isEmpty(pwd1) || TextUtils.isEmpty(pwd2)) {
            ToastUtil.showToast(this,"请输入密码");
            return false;
        }
        if(!pwd1.equals(pwd2)) {
            ToastUtil.showToast(this,"两次输入不一致");
            return false;
        }
        return true;
    }

    private void setCommitButtonState(boolean state) {
        if(state) {
            mBtn.setClickable(true);
            mBtn.setBackgroundResource(R.drawable.selector_background_retrieve_btn);
        } else {
            mBtn.setClickable(false);
            mBtn.setBackgroundResource(R.drawable.shape_background_retrieve_btn_unable);
        }
    }
}
